/*
 * @Author: your name
 * @Date: 2020-12-10 19:56:59
 * @LastEditTime: 2020-12-18 11:35:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \H5 2010 studyd:\phpStudy\WWW\proj_steam\src\js\register\register.js
 */
require(['../config'], () => {
    require(['header', 'footer', 'nav', 'cookie'], () => {
        class Register {
            constructor() {
                this.userRegister()
            }

            // 注册
            userRegister() {
                
                $('#registerBtn').click(function () {
                    $('section .content .register-contentBox form .inputBox:nth-of-type(1)').css({display:'none'})

                    const username = $('#username').val(),
                        password = $('#password').val()

                    // 先把 localStorage 上面的 userList 拿下来判断有没有数据
                    const userlist = JSON.parse(localStorage.getItem('userlist'))

                    if (userlist) {
                        // 如果进入 这个条件，就代表 list 存在
                        // 再判断 localStorage 保存的用户名 是否和我 当前的用户名一样

                        if($('#confirm').prop('checked') === false){
                            $('section .content .register-contentBox form .inputBox:nth-of-type(1)').slideDown(300)
                        }else{
                            const hasData = userlist.some(item => {
                                return item.username === username
                            })
                            if (hasData) {
                                $('#registed').css({display:'flex'})
                                $('#registedBtn').click(function(){
                                    $('#registed').css({display:'none'})
                                })

                            } else {
                                userlist.push({ username, password })

                                $('#register-success').css({display:'flex'})
                                $('#jumpBtn').click(function(){
                                    window.open('/html/login/login.html', '_self')
                                })
                            
                            }
                        }

                        localStorage.setItem('userlist', JSON.stringify(userlist))

                    } else {
                        // 进入这个条件 代表 userlist 没有数据

                        localStorage.setItem('userlist', JSON.stringify([{ username, password }]))

                        // alert('注册成功，点击确定跳转')
                        //     window.open('/html/login/login.html', '_self')

                        $('#register-success').css({display:'flex'})
                        $('#jumpBtn').click(function(){
                            window.open('/html/login/login.html', '_self')
                        })
                    }

                    
                })
            }
        }
        return new Register
    })
})

